package org.flik.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.flik.server.commonUtils.CommonUtils;
import org.flik.server.dto.TextLongPair;
import org.flik.server.dto.TicketDetail;
import org.flik.server.dto.TicketDetailComment;
import org.flik.server.entity.FlikUser;
import org.flik.server.entity.Ticket;
import org.flik.server.entity.TicketComment;
import org.flik.server.entity.TicketStatus;
import org.flik.server.entity.TicketType;

public class ObjectMapper {

	public static TicketDetail mapTicketDetail(Ticket t, Date modifiedDate,
			Collection<TicketDetailComment> comments,
			List<TextLongPair> projectTicketTypeList,
			List<TextLongPair> projectTicketStatusList, List<TextLongPair> projectUsers) {
		TicketDetail ticketDetail = new TicketDetail(
				t.getProject().getDescription(), 
				t.getTicketType().getDescription(), 
				t.getTicketStatus().getDescription(),
				CommonUtils.constructFullName(t.getAssignee().getFirstName(), t.getAssignee().getLastName()),
				CommonUtils.constructFullName(t.getReportedBy().getFirstName(), t.getReportedBy().getLastName()), 
				t.getTitle(), 
				t.getProject().getId(), 
				t.getTicketType().getId(), 
				t.getTicketStatus().getId(), 
				t.getAssignee().getId(),
				t.getId(), 
				t.getCreateDate(), 
				modifiedDate, 
				comments, 
				projectTicketTypeList, 
				projectTicketStatusList, 
				t.getTicketComment().getComment(), 
				projectUsers);
		return ticketDetail;
	}

	public static Collection<TicketDetailComment> mapCommentList(Collection<TicketComment> ticketComments) {
		List<TicketDetailComment> ticketDetailCommentList = new ArrayList<TicketDetailComment>();
		for (TicketComment tc: ticketComments){
			Collection<TicketDetailComment> replies = null;//= (tc.getReplies() == null?null:mapCommentList(tc.getReplies()));			
			if (tc.getReplyTo() != null)
			ticketDetailCommentList.add(new TicketDetailComment(tc.getId(), tc.getComment(), tc.getPostedOn(), 
					CommonUtils.constructFullName(tc.getFlikUser().getFirstName(), tc.getFlikUser().getLastName()), 
					replies));
		}
		return ticketDetailCommentList;
	}
	
	public static List<TextLongPair> mapProjectTicketStatusList(List<TicketStatus> projectStatusList) {
		List<TextLongPair> list = new ArrayList<TextLongPair>();
		for (TicketStatus projectStatus: projectStatusList)
			list.add(new TextLongPair(projectStatus.getId(), projectStatus.getDescription()));
		return list;
	}

	public static List<TextLongPair> mapProjectTicketTypeList(List<TicketType> projectTicketTypeList) {
		List<TextLongPair> list = new ArrayList<TextLongPair>();
		for (TicketType ticketType: projectTicketTypeList)
			list.add(new TextLongPair(ticketType.getId(), ticketType.getDescription()));
		return list;
	}


	public static List<TextLongPair>  mapProjectFlikUserList(List<FlikUser> projectFlikUserList) {
		List<TextLongPair> list = new ArrayList<TextLongPair>();
		for (FlikUser flikUser: projectFlikUserList)
			list.add(new TextLongPair(flikUser.getId(), CommonUtils.constructFullName(flikUser.getFirstName(), flikUser.getLastName())));
		return list;
	}
	
}
